package com.example.demo.demo.controller;

import com.example.demo.demo.dao.IStudentDao;
import com.example.demo.demo.entity.Student;
import com.example.demo.demo.service.StudentService;
import com.example.demo.demo.utlls.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author My
 * @date 2020/12/19 13:49
 */
@Api(tags = "/学生(student)管理类")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    IStudentDao studentDao;

    @Autowired
    StudentService studentService;

    /**
     * 不从数据库中查询
     * @return
     */
    @GetMapping("/list.do")
    public List<Student> list(){
        Student student = new Student("1","张三","男",19,"IS");
        List<Student> list = new ArrayList<>();
        list.add(student);
        return list;
    }

    /**
     * 从DB中，使用JPA工具查询该表所有数据
     * @return
     */
    @ApiOperation(value = "获取学生所有列表", notes = "获取学生所有列表")
    @GetMapping("/listByJPA.do")
    public List<Student> listByJPA(){
        List<Student> list = studentDao.findAll();
        return list;
    }


    /**
     * 根据条件查询，使用自定义SQL
     * @param name
     * @return
     */
    @ApiOperation(value = "根据学生姓名获取学生信息", notes = "根据学生姓名获取学生信息")
    @ApiImplicitParam(name = "name", value = "学生姓名", defaultValue = "张立", required = true)
    @GetMapping("/listByName.do")
    public List<Student> listByName(String name){
        List<Student> list = studentDao.listByCondition(name);
        return list;
    }

    @ApiOperation(value = "根据学生姓名获取学生信息", notes = "根据学生姓名获取学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学生姓名", defaultValue = "张立", required = true),
            @ApiImplicitParam(name = "age", value = "学生年龄", defaultValue = "19", required = true)
    })
    @GetMapping("/listByNameAndAge.do")
    public List<Student> listByNameAndAge(String name){
        List<Student> list = studentDao.listByCondition(name);
        return list;
    }


    /**
     * 新增（单个）
     * @param sno 学生学号
     * @return
     */
    @ApiOperation(value = "新增学生信息", notes = "新增学生信息")
    @ApiImplicitParam(name = "sno", value = "学生x学号", defaultValue = "12345678", required = true)
    @PostMapping("/save.do")
    public List<Student> save(String sno){

        //如果位空则赋值
        if(StringUtils.isEmpty(sno)){
            //设置name值
            sno = RandomUtil.newString(5);
        }
        studentService.save(sno);
        List<Student> list = studentDao.findAll();
        return list;
    }

    @ApiOperation(value = "新增学生信息2", notes = "新增学生信息2")
    @PostMapping("/saveEntity.do")
    public List<Student> saveEntity(Student student){
        studentDao.save(student);
        List<Student> list = studentDao.findAll();
        return list;
    }


    /**
     * 批量保存
     * @param no 新增学生数量
     * @return
     */

    @GetMapping("/batchSave.do")
    public List<Student> batchSave(Integer no){
        studentService.batchSave(no);
        List<Student> list = studentDao.findAll();
        return list;
    }
}
